#include "SoundManager.h"

SoundManager * SoundManager::soundManager;
SoundManager * SoundManager::getSoundManager()

{
	if (soundManager == NULL) soundManager = new SoundManager();
	return soundManager;
}

SoundManager::SoundManager(void)
{

}

SoundManager::~SoundManager(void)
{
}

void SoundManager::loadSound(std::string _file, std::string _alias, int _type, double _length )
{
	SoundManager * SM = getSoundManager();
	//0 = stream
	//!= 0  = sample  
	if (_type == 0){
		HSTREAM s = BASS_StreamCreateFile(false, (char*)_file.data(),0,0,0);
		SM->Streams.insert(std::pair<std::string,HSTREAM>(_alias,s));
		
	} else {
		//HSAMPLE s = BASS_SampleLoad(false,(char*)_file.data(),0,_length,10,BASS_SAMPLE_3D | BASS_SAMPLE_MONO);
		HSAMPLE s = BASS_SampleLoad(false,(char*)_file.data(),0,_length,30,0);
		Sample sample;
		sample.sample = s;
		SM->Samples.insert(std::pair<std::string,Sample>(_alias,sample));
		
		
	}
	int i = BASS_ErrorGetCode();
}

void SoundManager::playSound(std::string _alias, int _type )
{

	SoundManager * SM = getSoundManager();
	int i;
	if (_type == 0){
		std::map<std::string, HSTREAM>::iterator itST;
		itST = SM->Streams.find(_alias);
		if (itST != SM->Streams.end()){
			HSTREAM stream = itST->second;
			BASS_ChannelPlay(stream, true);
		}
	} else {
		std::map<std::string, Sample>::iterator itSA;
		itSA = SM->Samples.find(_alias);
		if (itSA != SM->Samples.end()){
			HSAMPLE  sample = itSA->second.sample;
			HCHANNEL channel = itSA->second.channel;
			if(channel == NULL){
				channel = BASS_SampleGetChannel(sample, false);
				if (channel == NULL){
					channel = NULL;
				}
				SM->setSamplerChannel(_alias,channel);
				i = BASS_ErrorGetCode();
				if(i!=0){
					i=0;
				}
			}
			itSA->second.channel = BASS_SampleGetChannel(sample, false);
 			BASS_ChannelPlay(itSA->second.channel, true);
		}
	}
	i = BASS_ErrorGetCode();
}

void SoundManager::stopSound(std::string _alias,int _type){

	SoundManager * SM = getSoundManager();

	if (_type == 0){
		std::map<std::string, HSTREAM>::iterator itST;
		itST = SM->Streams.find(_alias);
		if (itST != SM->Streams.end()){
			HSTREAM stream = itST->second;
			//BASS_Stream
		}
	} else {
		std::map<std::string, Sample>::iterator itSA;
		itSA = SM->Samples.find(_alias);
		if (itSA != SM->Samples.end()){
			HSAMPLE  sample = itSA->second.sample;
			BASS_SampleStop(sample);
		}
	}
}


Sample SoundManager::getSampleID(std::string _alias){
	SoundManager * SM = getSoundManager();
	std::map<std::string, Sample>::iterator itSA;
	itSA = SM->Samples.find(_alias);
	if (itSA != SM->Samples.end()){
		return(itSA->second);
	}
	Sample sample;
	return sample;
}

void SoundManager::setSamplerChannel(std::string _alias, HCHANNEL _channel){
	SoundManager * SM = getSoundManager();
	std::map<std::string, Sample>::iterator itSA;
	itSA = SM->Samples.find(_alias);
	if (itSA != SM->Samples.end()){
		itSA->second.channel = _channel;
	}
}